﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnimalCare2012.Model;

namespace AnimalCare2012
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        EntityConnection EntityCon = null;

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //IDbConnection con = GetConnection();

            //ProductionBLL provider = new ProductionBLL(con);
            if (EntityCon == null)
            {
                EntityCon = new EntityConnection(GetConnectionString());
            }
            ProductionBLL provider = new ProductionBLL(EntityCon);

            //ProductionBLL provider = new ProductionBLL();

            List<production> list = provider.Find(textBox1.Text);

            textBox1.Text = list.Count.ToString();
            production pro = list[0];

            var bindinglist = new BindingList<production>(list);

            var source = new BindingSource(bindinglist, null);

            dataGridView1.DataSource = source;
        }

        public string GetConnectionString()
        {
            SqlConnectionStringBuilder sqlbuilder = new SqlConnectionStringBuilder();

            EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();

            sqlbuilder.UserID = "sa";
            sqlbuilder.Password = "123456";
            sqlbuilder.InitialCatalog = "testDB";
            sqlbuilder.DataSource = "localhost";

            builder.Provider = "System.Data.SqlClient";
            builder.ProviderConnectionString = sqlbuilder.ToString();
            builder.Metadata = "res://*";

            return builder.ToString();
        }

        public IDbConnection GetConnection()
        {
            IDbConnection con = new SqlConnection(GetConnectionString());

            return con;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (EntityCon == null)
            {
                EntityCon = new EntityConnection(GetConnectionString());
            }
            ProductionBLL provider = new ProductionBLL(EntityCon);

            int i = provider.Insert(txtMaProduct.Text, txtTenProduct.Text, txtMaNhaCC.Text, txtTenNhaCC.Text);

        }

        private void button3_Click(object sender, EventArgs e)
        {

            if (EntityCon == null)
            {
                EntityCon = new EntityConnection(GetConnectionString());
            }
            ProductionBLL provider = new ProductionBLL(EntityCon);

            int i = provider.Update(txtMaProduct.Text, txtTenProduct.Text);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int id = int.Parse(textBox1.Text);

            if (EntityCon == null)
            {
                EntityCon = new EntityConnection(GetConnectionString());
            }
            ProductionBLL provider = new ProductionBLL(EntityCon);

            production p = provider.FindByKey(id);


            MessageBox.Show(p.pro_name);
        }
    }
}